package com.lagou.study.service.impl;

import cn.hutool.setting.dialect.Props;
import com.lagou.study.service.EmailService;
import com.lagou.study.util.MailUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;

import javax.mail.internet.MimeUtility;
import java.io.UnsupportedEncodingException;

@RefreshScope
@Service
public class EmailServiceImpl implements EmailService {

    // Hutool读取properties文件工具类
    private Props props = new Props("email.properties","UTF-8");

    @Value("${spring.mail.host}")
    String host;

    @Value("${spring.mail.username}")
    String username;

    @Value("${spring.mail.password}")
    String password;

    @Value("${spring.mail.protocol}")
    String protocol;

    @Value("${spring.mail.default-encoding}")
    String defaultEncoding;

    @Override
    public boolean sendCode(String email, String code) {
        try {
            return MailUtil.send(host,username,email, MimeUtility.encodeWord(props.getProperty("subject"), "UTF-8", "Q"),code,username,password);
        } catch (UnsupportedEncodingException e) {
            return false;
        }
    }
}
